#!/usr/bin/env bash

set -eo pipefail
CUR_DIR=$(readlink -f "$(dirname "$0")")

MIRRORS_SITE=http://repo.huaweicloud.com/ubuntu/
STUDIO_PKG=bmc-studio-1.1.66.250324-beta.deb
CANGJIE_PKG=Cangjie-0.53.13-linux_x64.tar.gz
STUDIO_PATH=/usr/share/bmc_studio
COMPLEX_RULE_PATH=${STUDIO_PATH}/server/data/complex_rules.json
BASE_RULE_PATH=${STUDIO_PATH}/server/data/base_rules.json
CI_REPO_PATH=/root/repo

function set_pip_source() {
  pip config set global.index-url https://mirrors.huaweicloud.com/repository/pypi/simple
}

function obs_get() {
  python3 "${CI_REPO_PATH}/ci-repos/.common_scripts/model_check_setup.py"
  if [ ! $? -eq 0 ]; then
    echo "安装依赖失败"
    exit 1
  fi
}

function install_studio() {
  wget https://repo.openubmc.cn/latest/tools/BMC%20Studio.zip -O studio.zip
  unzip ./studio.zip
  DEBIAN_FRONTEND=noninteractive AUTO_ACCEPT_LICENSE=true apt install -y ./*.deb
}

function install_cangjie() {
  apt install cangjie-runtime
}

function mask_rules() {
  # 先把所有规则设为生效，然后排除一些不需要的规则
  sed -i 's/"ciEnabled": false/"ciEnabled": true/g' ${COMPLEX_RULE_PATH}
  # 不生效规则，将该条规则的ciEnabled字段设置为false
  sed -i '/CSR属性引用Scanner对象检查/ { n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR拓扑总线配置正确性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置Connector对象正确性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR拓扑整体配置正确性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置精细化事件一致性检查/ { n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR自定义语法管道运算正确性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR中Scanner对象重复检查/ { n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置SmcDfxInfo对象的Period字段取值合理性检查/ { n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置Scanner对象正确性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR禁止同步Accessor对象检查/ { n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置Debounce对象有效性检查/ { n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置BoardRAS对象正确性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置CPU对象正确性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置Entity对象PowerState属性取值正确性的检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置Product对象正确性检查/ { n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置Chip对象有效性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置离散事件有效性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置NPU对象正确性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置FDMDomain对象正确性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置SmcDfxInfo对象正确性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR对象名称有效性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置PCIeCard对象正确性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置SmcDfxInfo对象有效性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置连续传感器有效性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR静态引用语法正确性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置Scanner对象有效性检查/ { n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR配置精细化事件有效性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/CSR中@Default属性配置正确性检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}
  sed -i '/传入到Anchor的总线有效性检查/ { n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${COMPLEX_RULE_PATH}

  
  # 不生效部分基础规则
  sed -i '/CSR对象重名检查/ { n;n;n;n;n;n;n;n;n;n;n;n;n; s/true/false/; }' ${BASE_RULE_PATH}
}

echo "start to prepare env"
chmod +x /root/repo/ci-repos/.common_scripts/common.sh
source /root/repo/ci-repos/.common_scripts/common.sh
prepare_git $1 $2
prepare_conan2 $3 $4
prepare_bingo
prepare_sdk
prepare_apt
set_pip_source
install_cangjie
install_studio
mask_rules

export CANGJIE_HOME=/opt/cangjie

cleanup() {
  echo 'studio检查不通过'
}

function die() {
  echo "模型校验失败"
  exit 1
}

function studio_check() {
  trap cleanup ERR
  export LD_LIBRARY_PATH=/usr/share/bmc_studio/server:${LD_LIBRARY_PATH}
  export LIBRARY_PATH=/usr/share/bmc_studio/server:${LIBRARY_PATH}
  repo_flag=""
  if [ -n "${conan_remote_name}" ]; then
    repo_flag="-r ${conan_remote_name}"
  fi
  cd ~/source_code/
  bingo check ${repo_flag} 2>&1 || die
  echo "模型检查通过"
}

studio_check
